/*===========================================================
 
.---=||||[ B L O O D Y ]|||||=====================----------....
|                                                              |
|    THE  M O S T   B L O O D Y  GAME EVER!                    |
|                                                              |
|    bloody harvest is coming... soon,                         |
|                                              heil Satan!     |
!                                                              !
.                                                              ,
~~~~-------------================================|||||||||==-~




Name-Space: {core, logic, editor}
Module: {draw, coll, scgr, ...}
Class: <name of class>
Last modification:  <author>, <time>

TODO:
<-------- todo #1 ---------->
<-------- todo #2 ---------->
<-------- todo #3 ---------->
............
<-------- todo #n ---------->

FIX:
<-------- fix #1 ----------->
<-------- fix #1 ----------->
..........
<-------- fix #n ----------->




 
 
============================================================*/
#include <GL/glew.h>

#include "core/draw/Device.h"
#include "core/exceptions.h"

using namespace std;

namespace core
{
	namespace draw
	{


		Device::Device(bool initialize)
		{
			screen = 0;
			nLights = 0;
			//textureManager = new TextureManager::();
			
			if (initialize)
			{
				init();
			}
			
			font = 0;
			
		}
		
		Device::~Device()
		{
			if (screen) 
				SDL_FreeSurface(screen);

		}


		void Device::init()
		{

// Win32 Specific setting
#ifdef W32		
			
		
			if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
				throw InvalidOperationException("SDL Initialization failed");
			atexit(SDL_Quit);
		
// Other OS setting
#else

			if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTTHREAD) < 0)
				throw InvalidOperationException("SDL Initialization failed");
			atexit(SDL_Quit);
			


#endif
// common setting




		}		
		void Device::initGL()
		{
			glewInit();

			glEnable(GL_DEPTH_TEST);
			//glEnable(GL_ALPHA_);
			glDepthFunc(GL_LEQUAL);
			glClearDepth(1.0f);
			glClearColor(0, 0, 0, 0);
			glShadeModel(GL_SMOOTH);
			glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
			glEnable(GL_TEXTURE_2D);
			glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
			/*glEnableClientState(GL_VERTEX_ARRAY);
			glEnableClientState(GL_NORMAL_ARRAY);
			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
			glEnableClientState(GL_INDEX_ARRAY);*/
		
			font = new Font("data/font/font.txt", "data/font/font.tga");

			GLint nl = 0;
			glGetIntegerv(GL_MAX_LIGHTS, &nl);
			nLights = nl;
			cout << "max lights: " << nLights << endl;
			for (u32 i = 0; i < nLights; i++)
			{
				lights.push_back(Light(i));
			}

		}
		
		void Device::setupMatrices()
		{
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			gluPerspective(90, 800/(600.0f), 0.01, 1000);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();
		}
		
		void Device::beginScene()
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glLoadIdentity();
		
			setupMatrices(); // rek bych ze tohle se vola zbytecne ;-)
		}
		
		void Device::endScene()
		{
			SDL_GL_SwapBuffers();
		}
		
		
		
		void Device::createWindow(int w, int h, bool fullscreen)
		{
			SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
			SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
			
			screen = SDL_SetVideoMode(w, h, 32, (fullscreen) ? SDL_OPENGL | SDL_FULLSCREEN : SDL_OPENGL);
			if (!screen)
				InvalidOperationException("Error initializing video mode");
			
			cerr << "Creating window " << w << "x" << h << "x" << 16 << endl;	
			glViewport(0, 0, w, h);
				
		}
		
		


	
	}
}
